package fem2.gui;

//import inf.timer.Timer;

import java.util.HashSet;
import java.util.Set;

import fem2.Model;

public class ModelHolder {

	private Model model;
	private boolean solutionAvailable = false;
	private Set<ModelObserver> observers_ = new HashSet<ModelObserver>();

	// private static Timer t = Timer.getInstance();

	/**
	 * Adds the specified observer.
	 * 
	 * @param o
	 *            observer to add
	 */
	public void addObserver(ModelObserver o) {
		observers_.add(o);
	}

	private void fireModelChangedEvent() {
		for (ModelObserver o : observers_) {
			o.solutionChanged(this);
		}
	}

	/**
	 * Notify observers that the solution data changed.
	 */
	public void fireSolvedEvent() {
		for (ModelObserver o : observers_) {
			o.solutionChanged(this);
		}
	}

	public Model getModel() {
		return model;
	}

	public void removeObserver(ModelObserver o) {
		observers_.remove(o);
	}

	public void setModel(Model model) {
		this.model = model;
		fireModelChangedEvent();
	}

	public boolean solutionAvailable() {
		boolean b = this.solutionAvailable
				|| (this.model != null && this.model.getMesh().getNode(0).getUHatLocal() != null);
		return b;
	}

	public void solvex() {
		this.solutionAvailable = true;
		fireSolvedEvent();
	}

	// public void solve() throws SolveFailedException {
	// t.start("Solve");
	// this.model.solve();
	// t.stop();
	// this.solutionAvailable = true;
	// fireSolvedEvent();
	// }
}
